package com.sysu.homework.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.homework.DAO.StudentDAO;
import com.sysu.homework.model.Group;
import com.sysu.homework.model.Student;
import com.sysu.homework.service.StudentService;

@Component("groupAction")
@Scope("prototype")
public class GroupAction extends ActionSupport {
	private Group myGroup ;
	private List<Group> groups = new ArrayList<Group>();
	private int groupId;
	
	private StudentService studentService;
	HttpSession session = null;
	private String role;
	
	public GroupAction(){
		session = ServletActionContext.getRequest().getSession();
		setRole(session.getAttribute("role").toString());
	}
	
	@Override
	public String execute() throws Exception {
		myGroup = showMyGroup();
		groups = showGroups();
		return "success";
	}
	
	public Group showMyGroup() {
		int userId = (Integer)session.getAttribute("user");
		if(userId>0 && role.equals("student"))
			return studentService.getStudentById(userId).getGroup();
		return null;
	}
	
	public List<Group> showGroups(){
		return studentService.getAllGroups();
	}
	 
	public String addToGroup() {
		myGroup = showMyGroup();
		groups = showGroups();
		int userId = (Integer)session.getAttribute("user");
		Student student = studentService.getStudentById(userId);
		if (student.getGroup() == null) 
		{
			if(studentService.addToGroup(userId, groupId)>0)
				return "completed";
		}
		return "fail";
	}
	
	public String removeFromGroup() {
		myGroup = showMyGroup();
		groups = showGroups();
		int userId = (Integer)session.getAttribute("user");
		if (groupId != 0){
			if(studentService.removeFromGroup(userId, groupId)>0)
				return "completed";
		}
		return "fail";
	}
	
	public String addLock() {
		myGroup = showMyGroup();
		groups = showGroups();
		if (groupId != 0 && (role.equals("ta")||role.equals("teacher"))){
			if (studentService.addLockToGroup(groupId)>0);
				return "success";
		}
		return "fail";
	}
	public String removeLock() {
		myGroup = showMyGroup();
		groups = showGroups();
		if (groupId != 0 && (role.equals("ta")||role.equals("teacher"))){
			if (studentService.removeLockToGroup(groupId)>0);
				return "success";
		}
		return "fail";
	}
	

	public Group getMyGroup() {
		return myGroup;
	}

	public void setMyGroup(Group myGroup) {
		this.myGroup = myGroup;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getRole() {
		return role;
	}

	
	
	
}
